﻿Imports MICROFORMA.BEL
Imports MICROFORMA.BLL


Public Class frmAddDetalleLote


    Private _ListDetalle As List(Of DetalleLoteBE)
    Public Property ListDetalle() As List(Of DetalleLoteBE)
        Get
            Return _ListDetalle
        End Get
        Set(ByVal value As List(Of DetalleLoteBE))
            _ListDetalle = value
        End Set
    End Property
    Public BolSoloItem As Boolean
    Public ItemDetalle As DetalleLoteBE

    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        Me.Close()
    End Sub

    Private Sub btnAceptar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAceptar.Click
        If ValidateForm() Then

            If BolSoloItem Then
                ItemDetalle = setEntity()
            Else
                ListDetalle.Add(setEntity())
            End If
            
            Call MessageMistake("La operación se llevo a cabo con exito.", True)
            Me.Close()

        End If
    End Sub

    Private Function ValidateForm() As Boolean
        'Pestaña General
        If Trim(txtCantDocu.Text) = "" Then
            Call MessageMistake("Por Favor ingrese la cantidad.")
            txtCantDocu.Focus()
            Exit Function
        End If
        If Trim(txtCantFollios.Text) = "" Then
            Call MessageMistake("Por favor selecione un tipo de solicitud.")
            Exit Function
        End If

        If txtCantDocu.Text > txtCantFollios.Text Then
            Call MessageMistake("La cantidad de documentos no puede ser mayor a la cantidad de folios.")
            Exit Function

        End If

        ValidateForm = True

    End Function


    Private Sub frmAddDetalleLote_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If ListDetalle Is Nothing Then
            ListDetalle = New List(Of DetalleLoteBE)
        End If
        Call CargarTiposDocumentales()
    End Sub
    Sub CargarTiposDocumentales()
        Dim tiposDocBl As New TypeDocBL
        Dim _DataTableTiposDoc As DataTable = tiposDocBl.GetListAll
        Dim StrTextoAdd As String

        For Each dtrFila As DataRow In _DataTableTiposDoc.Rows
            StrTextoAdd = dtrFila("IdPlantilla") & "-" & dtrFila("Nombre")
            cmbTipoDocumento.Items.Add(StrTextoAdd)
        Next


    End Sub
    Private Function setEntity() As DetalleLoteBE

        Dim StrNombreTipoDocumental As String
        Dim StrIdTipoDocumental As String = cmbTipoDocumento.Text
        StrNombreTipoDocumental = StrIdTipoDocumental.Substring(StrIdTipoDocumental.IndexOf("-"))
        StrIdTipoDocumental = StrIdTipoDocumental.Substring(0, StrIdTipoDocumental.IndexOf("-"))



        Dim _DetalleLoteBE As New DetalleLoteBE
        _DetalleLoteBE.IdTipoContenedor = 1
        _DetalleLoteBE.NombreTipoContenedor = cmbTipoContenedor.Text
        _DetalleLoteBE.IdTipoDocumento = StrIdTipoDocumental
        _DetalleLoteBE.NombreTipoDocumento = StrNombreTipoDocumental
        _DetalleLoteBE.CantidadDocumentos = txtCantDocu.Text
        _DetalleLoteBE.CantidadFolios = txtCantFollios.Text
        _DetalleLoteBE.Comentarios = txtComentarios.Text
        _DetalleLoteBE.EstadoDetalle = cbConforme.Checked
        Return _DetalleLoteBE

    End Function

End Class